package priv.pfz.basedao.mybatis.sqlprovider;

import org.apache.ibatis.builder.annotation.ProviderContext;
import priv.pfz.basedao.Utils;
import priv.pfz.basedao.info.DaoInfo;
import priv.pfz.basedao.mybatis.DaoInfoCache;
import priv.pfz.basedao.sharding.ShardingInfo;
import priv.pfz.basedao.sharding.ShardingManager;
import priv.pfz.basedao.sqlbuilder.SQL;

/**
 * SELECT a,b,c FROM table WHERE id in ids
 * @author yyjxp
 * 2024/1/28 23:55
 */
public class ListByIdsSqlProvider extends BaseSqlProvider {

    public String provideSql(Iterable<?> ids, ProviderContext context) {
        return provideShardingSql(ids, null, context);
    }

    public String provideShardingSql(Iterable<?> ids, ShardingInfo shardingInfo, ProviderContext context) {
        Utils.checkNotEmpty(ids, "ids不能为空");
        DaoInfo daoInfo = DaoInfoCache.getDaoInfo(context.getMapperType());
        ShardingManager shardingManager = new ShardingManager(daoInfo, shardingInfo);

        SQL sql = new SQL();
        sql.SELECT(daoInfo.getSelectColumns());
        sql.FROM(shardingManager.getShardingTable());
        sql.WHERE(getInList("id", "arg0", ids));
        if (daoInfo.isEnableLogicDelete()) {
            sql.WHERE(getLogicDeleteWhere(daoInfo));
        }
        String sqlStr = sql.toString();
        sqlStr += shardingManager.getThirdShardingStr();
        System.out.println(sqlStr);
        return sqlStr;
    }
}
